<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="isOk"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="rows">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="data">
                  <el-button size="small" type="success" @click="assignPerm(data.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="edit(data.row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="del(data.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination layout="prev,pager,next" :total="total" :page-size="pages.pagesize" @current-change="changePages" />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="Information.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="Information.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="Information.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="Information.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog :title="roleForm.id? '编辑角色':'新增角色'" :visible.sync="showDialog" @close="btnCancel">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="add">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <el-dialog title="分配权限" :visible="showPerm" @close="btnPermCancel">
        <!--
          default-expand-all 默认展开所有
          show-checkbox 显示多选框
          check-strictly 断开父子节点的选项关联
        -->
        <el-tree
          ref="tree"
          node-key="id"
          :data="permList"
          :props="{label: 'name'}"
          default-expand-all
          show-checkbox
          check-strictly
        />
        <el-button size="small" @click="btnPermCancel">取消</el-button>
        <el-button size="small" type="primary" @click="btnPermOk">确定</el-button>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { addRole, business, delRole, editRole, getRole, getRoleDetail } from '@/api/business'
import { getPermList, savePerm } from '@/api/permission'
import { getInfo } from '@/api/user'
import { getChildren } from '@/utils/getDepartmentChildren'
export default {
  data() {
    return {
      Information: {},
      total: null,
      showPerm: false,
      rows: [],
      pages: {
        pagesize: 2,
        page: 1
      },
      permList: [],
      showDialog: false,
      // 专门接收新增或者编辑的编辑的表单数据
      roleForm: {
        name: '',
        description: ''
      },
      roleId: '',
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      }
    }
  },
  async created() {
    const { companyId } = await getInfo()
    const res = await business(companyId)
    this.Information = res
    this.Role()
    this.getPermList()
  },
  methods: {
    async btnPermOk() {
      await savePerm({
        id: this.roleId,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      this.$message.success('修改成功')
      this.btnPermCancel()
    },
    btnPermCancel() {
      this.showPerm = false
    },
    async getPermList() {
      this.permList = getChildren(await getPermList(), '0')
      console.log(133, this.permList)
    },
    async assignPerm(id) {
      this.roleId = id
      const { permIds } = await getRoleDetail(id)
      console.log(permIds)
      this.showPerm = true
      this.$nextTick(() => {
        this.$refs.tree.setCheckedKeys(permIds)
      })
    },
    async Role() {
      const { rows, total } = await getRole(this.pages)
      this.total = total
      this.rows = rows
    },
    async changePages(nv) {
      this.pages.page = nv
      this.Role()
    },
    async del(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message.success('删除成功')
        delRole(id)
        if (this.rows.length === 1) {
          this.pages.page -= 1
        }
        this.Role()
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    isOk() {
      this.showDialog = true
    },
    btnCancel() {
      this.$refs.roleForm.resetFields()
      this.showDialog = false
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    async add() {
      if (!this.roleForm.id) {
        await addRole(this.roleForm)
        this.Role()
        this.showDialog = false
        this.$message.success('添加成功😀')
      } else {
        await editRole(this.roleForm.id, this.roleForm)
        this.Role()
        this.showDialog = false
        this.$message.success('编辑成功😀')
      }
    },
    edit(data) {
      this.roleForm = data
      this.showDialog = true
    }
  }
}
</script>

<style>

</style>
